from plotly.graph_objs import Bar, Layout
from plotly import offline

from die import Die

# 创建两个D6(面数为6的骰子)
die_1 = Die()
# die_2 = Die()
die_2 = Die(10)

# 掷几次骰子并将结果存储在一个列表中
results = []
for roll_num in range(50_000):
    result = die_1.roll() + die_2.roll() # 将两个投掷结果相加存储
    results.append(result)

# print(results)

# 分析结果
frequencies = []
max_result = die_1.num_sides + die_2.num_sides # 投掷可能出现的最大结果
for value in range(1, max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)

# print(frequencies)

# 对结果进行可视化
# x_values = list(range(1, die.num_sides+1))
x_values = list(range(2, max_result+1))
data = [Bar(x=x_values, y=frequencies)]

x_axis_config = {'title': '结果', 'dtick': 1} # dtick制定了坐标轴上刻度显示的间距，设置dtick=1即设置所有刻度值都显示
y_axis_config = {'title': '结果的频率'}
# my_layout = Layout(title='掷两个D6 1000次的结果',
#         xaxis=x_axis_config, yaxis=y_axis_config)
my_layout = Layout(title='掷一个D6和一个D10 50 000次的结果',
        xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6_d10.html')